<template>
  <div class="rank" ref="rank">
    <scroll :data="topList" class="toplist" ref="toplist">
      <ul>
        <li class="item" :key="list" v-for="(item, list) in topList" @click="selectItem(item)">
          <div class="icon">
            <img width="100" height="100" v-lazy="item.pic"/>
          </div>
          <ul class="songlist">
            <li class="song" :key="index" v-for="(song, index) in item.list" v-if="index < 3" >
              <span>{{index + 1}}</span>
              <span>{{song.name}}</span>
            </li>
          </ul>
        </li>
      </ul>
      <div class="loading-container" v-show="!topList.length">
        <loading></loading>
      </div>
    </scroll>
    <router-view></router-view>
  </div>
</template>

<script type="text/ecmascript-6">
import Scroll from 'base/scroll/scroll'
import Loading from 'base/loading/loading'
import { playlistMixin } from 'common/js/mixin'
import { mapMutations } from 'vuex'
import { getAllRankList } from 'api/index'
import { createRank } from "common/js/rank";

export default {
  mixins: [playlistMixin],
  created() {
    setTimeout(() => {
      this._getTopList()
    }, 1000)
  },
  data() {
    return {
      topList: []
      //list: []
    }
  },
  methods: {
    handlePlaylist(playlist) {
      const bottom = playlist.length ? '60px' : ''
      this.$refs.rank.style.bottom = bottom
      this.$refs.toplist.refresh()
    },
    selectItem(item) {
      this.$router.push({
        path: `/rank/${item.id}`
      })
      this.setTopList(item)  //点击排行榜，把相应的list传入state的topList，top-list就能取到当前的topList
    },
    _getTopList() {
      getAllRankList().then((res) => {
        if(res) {
          this.topList = this._normalizeRank(res)
        }
      })
    },
    _normalizeRank(list) {
      let ret = []
      let reu = []
      list.forEach((item) => {
        if(item.id) {
          ret.push(createRank(item))
          //console.log(ret)
        }
      });
      ret.forEach((item) => {
        item.list.then((res)=>{
          reu = res
          item.list = reu
        })
      })
      //console.log(ret)
      return ret

    },
    ...mapMutations({
      setTopList: 'SET_TOP_LIST'
    })
  },
  components: {
    Scroll,
    Loading
  }
}

</script>

<style scoped lang="stylus" rel="stylesheet/stylus">
@import "~common/stylus/variable"
@import "~common/stylus/mixin"

.rank
  position: fixed
  width: 100%
  top: 88px
  bottom: 0
  .toplist
    height: 100%
    overflow: hidden
    .item
      display: flex
      margin: 0 20px
      padding-top: 20px
      height: 100px
      &:last-child
        padding-bottom: 20px
      .icon
        flex: 0 0 100px
        width: 100px
        height: 100px
      .songlist
        flex: 1
        display: flex
        flex-direction: column
        justift-content: center
        padding: 0 20px
        height: 100px
        overflow: hidden
        background: $color-highlight-background
        color: $color-text-d
        font-size: $font-size-small
        .song
          no-wrap()
          line-height: 32px

    .loading-container
      position: absolute
      width: 100%
      top: 50%
      transform: translateY(-50%)

</style>
